package com.uenpay.im.ui.old.activity.personal;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.callback.OnCommonCallBack;
import com.uenpay.im.invokeitems.person.UpdateInfoInvokItem;
import com.uenpay.util.app.base.BaseActivity;

/**
 * 修改性别
 *
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class UpdateSexActivity extends BaseActivity {

    private View loading;
    private ImageView boy_iv, girl_iv, all_iv;
    private int sex = 0;
    private boolean isUpdatePersonalInfo = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_update_sex_layout;
    }

    @Override
    public void initView(View view) {
        sex = getIntent().getIntExtra("sex_type", 0);
        isUpdatePersonalInfo = getIntent().getBooleanExtra(
                "isUpdatePersonalInfo", isUpdatePersonalInfo);
        initView();
        initListener();
        if (sex == 1) {
            girl_iv.setVisibility(View.GONE);
            all_iv.setVisibility(View.GONE);
            boy_iv.setVisibility(View.VISIBLE);
        } else if (sex == 0) {
            boy_iv.setVisibility(View.GONE);
            all_iv.setVisibility(View.GONE);
            girl_iv.setVisibility(View.VISIBLE);
        } else if (sex == 2) {
            boy_iv.setVisibility(View.GONE);
            girl_iv.setVisibility(View.GONE);
            all_iv.setVisibility(View.VISIBLE);
        }

        if (!isUpdatePersonalInfo) {
            findViewById(R.id.update_sex_all_layout).setVisibility(View.VISIBLE);
            findViewById(R.id.all_view).setVisibility(View.VISIBLE);
        }
    }

    protected void initView() {
        ((TextView) findViewById(R.id.title_textview)).setText(getResources()
                .getString(R.string.sex));
        boy_iv = (ImageView) findViewById(R.id.update_sex_boy_iv);
        girl_iv = (ImageView) findViewById(R.id.update_sex_girl_iv);
        all_iv = (ImageView) findViewById(R.id.update_sex_all_iv);
        loading = findViewById(R.id.loading);
    }

    protected void initListener() {
        //返回键
        findViewById(R.id.back_layout).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
        findViewById(R.id.update_sex_all_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        girl_iv.setVisibility(View.GONE);
                        boy_iv.setVisibility(View.GONE);
                        all_iv.setVisibility(View.VISIBLE);
                        Intent data = new Intent();
                        data.putExtra("sex", 2);
                        setResult(RESULT_OK, data);
                        finish();
                    }
                });
        findViewById(R.id.update_sex_boy_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        girl_iv.setVisibility(View.GONE);
                        boy_iv.setVisibility(View.VISIBLE);
                        all_iv.setVisibility(View.GONE);
                        if (isUpdatePersonalInfo) {
                            updateSex(1);
                        } else {
                            Intent data = new Intent();
                            data.putExtra("sex", 1);
                            setResult(RESULT_OK, data);
                            finish();
                        }
                    }
                });
        findViewById(R.id.update_sex_girl_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        boy_iv.setVisibility(View.GONE);
                        all_iv.setVisibility(View.GONE);
                        girl_iv.setVisibility(View.VISIBLE);
                        if (isUpdatePersonalInfo) {
                            updateSex(0);
                        } else {
                            Intent data = new Intent();
                            data.putExtra("sex", 0);
                            setResult(RESULT_OK, data);
                            finish();
                        }
                    }
                });
    }

    private void updateSex(final int value) {
        loading.setVisibility(View.VISIBLE);
        UpdatePersonInfoCommonHandle.udpatePersonInfo("sex", value + "",
                new OnCommonCallBack() {

                    @Override
                    public void onSuccess(Object result) {
                        myHandler.sendEmptyMessage(0);
                        UpdateInfoInvokItem.UpdateInfoInvokItemResult results = (UpdateInfoInvokItem.UpdateInfoInvokItemResult) result;
                        if (results != null && results.status == HttpInvokeResultCode.RESULT_OK) {
                            IMApplication.showToast(results.msg);
                            Intent data = new Intent();
                            data.putExtra("sex", value);
                            setResult(RESULT_OK, data);
                            finish();
                        }
                    }

                    @Override
                    public void onFailed(String reason) {
                        myHandler.sendEmptyMessage(0);
                        IMApplication.showToast(reason);
                    }
                });
    }

    private Handler myHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == 0) {
                if (loading != null) {
                    loading.setVisibility(View.GONE);
                }
            }
        }

        ;
    };
}
